package com.nanyang.tms.manager.waybill.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.forecast.ForecastReceiverDao;
import com.nanyang.tms.dao.forecast.ForecastSenderDao;
import com.nanyang.tms.dao.receipt.ReceiptRelationDao;
import com.nanyang.tms.dao.waybill.WaybillDao;
import com.nanyang.tms.domain.forecast.ForecastReceiverVO;
import com.nanyang.tms.domain.forecast.ForecastSenderVO;
import com.nanyang.tms.domain.receipt.ReceiptRelationVO;
import com.nanyang.tms.domain.waybill.WaybillQuery;
import com.nanyang.tms.domain.waybill.WaybillReceiverSenderVO;
import com.nanyang.tms.domain.waybill.WaybillVO;
import com.nanyang.tms.manager.waybill.WaybillManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component("waybillManager")
public class WaybillManagerImpl implements WaybillManager {
    @Autowired
    private WaybillDao waybillDao;
    @Autowired
    private ReceiptRelationDao receiptRelationDao;
    @Autowired
    private ForecastSenderDao forecastSenderDao;
    @Autowired
    private ForecastReceiverDao forecastReceiverDao;
    @Override
    public PageBean<WaybillVO> listWaybill(WaybillQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = waybillDao.countWaybill(query);
            PageBean<WaybillVO> page = new PageTableBean<WaybillVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(waybillDao.listWaybill(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @Override
    public WaybillReceiverSenderVO receiverSenderByWaybillCode(String waybillCode){
       try{
            WaybillReceiverSenderVO waybillReceiverSenderVO = new WaybillReceiverSenderVO();
            ReceiptRelationVO receiptRelationVO = receiptRelationDao.selectByWaybillCode(waybillCode);
            if(Objects.isNull(receiptRelationVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            ForecastSenderVO forecastSenderVO = forecastSenderDao.selectByCode(receiptRelationVO.getForecastCode());
            if(Objects.isNull(forecastSenderVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
           waybillReceiverSenderVO.setForecastSenderVO(forecastSenderVO);

            ForecastReceiverVO forecastReceiverVO = forecastReceiverDao.selectByCode(receiptRelationVO.getForecastCode());
            if(Objects.isNull(forecastReceiverVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
           waybillReceiverSenderVO.setForecastReceiverVO(forecastReceiverVO);
            return  waybillReceiverSenderVO;

        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
